﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CircleDrawer
{
    /// <summary>
    /// Interaction logic for NewFileWindow.xaml
    /// </summary>
    public partial class NewFileWindow : Window
    {
        public int Width { get; set; }
        public int Height { get; set; }

        public NewFileWindow()
        {
            InitializeComponent();
            btnOK.IsDefault = true;
            btnCancel.IsCancel = true;

            this.Width = 0;
            this.Height = 0;

            InitializeChooseSizesComboBox();
        }
  
        private void InitializeChooseSizesComboBox()
        {
            PaneSize custom = new PaneSize();
            custom.Name = "Custom";
            cbPaneSizes.Items.Add(custom);
            cbPaneSizes.DisplayMemberPath = "Name";
            cbPaneSizes.SelectedItem = cbPaneSizes.Items[0];

            PaneSize paneSize1280x800 = new PaneSize();
            paneSize1280x800.Name = "1280x800";
            paneSize1280x800.Width = 1280;
            paneSize1280x800.Height = 800;
            cbPaneSizes.Items.Add(paneSize1280x800);
            cbPaneSizes.DisplayMemberPath = "Name";

            PaneSize paneSize1024x768 = new PaneSize();
            paneSize1024x768.Name = "1024x768";
            paneSize1024x768.Width = 1024;
            paneSize1024x768.Height = 768;
            cbPaneSizes.Items.Add(paneSize1024x768);

            PaneSize paneSize800x600 = new PaneSize();
            paneSize800x600.Name = "800x600";
            paneSize800x600.Width = 800;
            paneSize800x600.Height = 600;
            cbPaneSizes.Items.Add(paneSize800x600);

            cbPaneSizes.SelectionChanged += cbPaneSizes_SelectionChanged;
        }

        void cbPaneSizes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PaneSize selectedItem = cbPaneSizes.SelectedItem as PaneSize;

            if (selectedItem.Name != "Custom")
            {
                this.tbHeight.Text = selectedItem.Height.ToString();
                this.tbHeight.IsEnabled = false;

                this.tbWidth.Text = selectedItem.Width.ToString();
                this.tbWidth.IsEnabled = false;
            }
            else
            {
                this.tbHeight.IsEnabled = true;
                this.tbWidth.IsEnabled = true;
            }
        }

        private int GetValueFromTextBox(TextBox textBox)
        {
            int result = 0;
            bool parseable = int.TryParse(textBox.Text, out result);
            if (!parseable)
            {
                throw new ArgumentException("You must enter valid number for width and height!");
            }
            return result;
        }

        private void btnOK_Click_1(object sender, RoutedEventArgs e)
        {
            PaneSize selectedItem = cbPaneSizes.SelectedItem as PaneSize;

            if (selectedItem.Name == "Custom")
            {
                try
                {
                    this.Width = GetValueFromTextBox(this.tbWidth);
                    this.Height = GetValueFromTextBox(this.tbHeight);
                }
                catch (ArgumentException exc)
                {
                    MessageBox.Show(exc.Message);
                    return;
                }
            }
            else
            {
                this.Width = selectedItem.Width;
                this.Height = selectedItem.Height;
            }

            DialogResult = true;
            CloseWindow();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            CloseWindow();
        }

        private void CloseWindow()
        {
            this.Close();
        }
    }
}
